package com.hopu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopu.exception.BusinessException;
import com.hopu.mapper.RoleMapper;
import com.hopu.pojo.Role;
import com.hopu.service.RoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author YwaiX
 * @since 2024-09-12
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    private RoleMapper roleMapper;

    @Override
    public void update(Role role) {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<Role>().eq(Role::getRoleName, role.getRoleName());
        Role role1 = getOne(wrapper);
        if (role1 != null && role1.getId().equals(role.getId())) {
            throw new BusinessException("该角色名已存在，请更换其他角色名");
        }
        updateById(role);
    }

    @Override
    public void updateStatus(Role role) {
        if (role.getId() == null) {
            throw new BusinessException("ID不能为空");
        }
        baseMapper.updateById(role);
    }

    @Override
    public String add(Role role) {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<Role>().eq(Role::getRoleName, role.getRoleName());
        Role role1 = getOne(wrapper);
        if (role1 != null) {
            throw new BusinessException("该角色名已存在，请更换其他角色名");
        }
        save(role);
        return "添加成功";
    }

    @Override
    public List<Role> getRoleById(Integer roleId) {
        return roleMapper.getRoleById(roleId);
    }

    @Override
    public void deleteStaff(List<Integer> ids) {
        baseMapper.deleteByIds(ids);
    }


}
